var fs = require('fs')
var student = require('./student')
var express = require('express')

var router = express.Router()

router.get('/students', function (req, res) {
    student.getList(function (err, students) {
        if (err) {
            return res.status(500).send('Server error.')
        }
        res.render('index.html', {
            fruits: [
                '苹果',
                '香蕉',
                '橘子'
            ],
            students: students
        })
    })
})
router.get('/add', function (req, res) {
    res.render('add.html')
})
router.post('/doadd', function (req, res) {
    student.addList(req.body, function (err) {
        if (err) {
            return res.status(500).send('Server error')
        }
        res.redirect('/students')
    })

})

router.get('/edit', function (req, res) {
    student.getStuByid(parseInt(req.query.id), function (err, student) {
        if (err) {
            return res.status(500).send('Server error')
        }
        res.render('edit.html', {
            student: student
        })
    })
})

router.post('/doedit', function (req, res) {
    student.editList(req.body, function (err) {
        if (err) {
            return res.status(500).send('error')
        }
        res.redirect('/students')
    })
})

router.get('/delete', function (req, res) {
    student.deleteList(req.query.id, function (err) {
        if (err) {
            return res.status(500).send('Server error')
        }
        res.redirect('/students')
    })
})

module.exports = router;